package com.casework
class Nace {
    String description
    String code
    NaceCategory naceCategory
    static transients = ['activeNaceCategories']

    static searchable = {
        only = ['description', 'code']
    }

    static mapping = {
        description type:'text'
    }

    static constraints = {
        description nullable: true
    }

    static def getActiveNaceCategories(){
        def descriptions = LegalCaseNaceFullDescription.withCriteria {
            projections {
                    distinct "description"
                }
            cache true
        }
        return descriptions.collect{it.naceCategory}.unique().sort{a, b -> a.shortDescription <=> b.shortDescription}
    }


    void beforeDelete(){
        LegalCase.withNewSession {
            def caseNaceFullDescription = LegalCaseNaceFullDescription.findAllByDescription(this)
            caseNaceFullDescription*.delete()
        }
    }

    String toString(){
        "$code - $description"
    }
}
